package com.yuwj.chinaa.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.yuwj.chinaa.R;
import com.yuwj.chinaa.adapter.FragmentTabAdapter;
import com.yuwj.chinaa.base.BaseActivity;
import com.yuwj.chinaa.fragment.FragmentOrderStateAskingBuy;
import com.yuwj.chinaa.fragment.FragmentOrderStateComplaining;
import com.yuwj.chinaa.fragment.FragmentOrderStateFinished;
import com.yuwj.chinaa.fragment.FragmentOrderStateSelling;
import com.yuwj.chinaa.fragment.FragmentOrderStateTrading;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class OrderBaseListActivity extends BaseActivity {
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.vp_container)
    ViewPager viewPager;
    @BindView(R.id.tl_foot)
    TabLayout tabLayout;

    FragmentOrderStateAskingBuy fragmentOrderStateAskingBuy;//订单状态之求购中
    FragmentOrderStateSelling fragmentOrderStateSelling;//订单状态之出售中
    FragmentOrderStateTrading fragmentOrderStateTrading;//订单状态之交易中
    FragmentOrderStateFinished fragmentOrderStateFinished;//订单状态之已完成
    FragmentOrderStateComplaining fragmentOrderStateComplaining;//订单状态之申诉中

    List<Fragment> fragmentList;
    FragmentTabAdapter tabAdapter;
    List<String> titleList;
    int selectedIndex = 0;


    public static Intent createIntent(Context context) {
        Intent intent = new Intent(context, OrderBaseListActivity.class);
        return intent;
    }


    @Override
    public int setCustomContentViewResourceId() {
        return R.layout.activity_order_base_list;
    }

    @Override
    public void initParam() {
        titleList = new ArrayList<>();
        titleList.add("求购中");
        titleList.add("出售中");
        titleList.add("交易中");
        titleList.add("已完成");
        titleList.add("申诉中");


        fragmentOrderStateAskingBuy = FragmentOrderStateAskingBuy.createInstance();
        fragmentOrderStateSelling = FragmentOrderStateSelling.createInstance();
        fragmentOrderStateTrading = FragmentOrderStateTrading.createInstance();
        fragmentOrderStateFinished = FragmentOrderStateFinished.createInstance();
        fragmentOrderStateComplaining = FragmentOrderStateComplaining.createInstance();

        fragmentList = new ArrayList<>();
        fragmentList.add(fragmentOrderStateAskingBuy);
        fragmentList.add(fragmentOrderStateSelling);
        fragmentList.add(fragmentOrderStateTrading);
        fragmentList.add(fragmentOrderStateFinished);
        fragmentList.add(fragmentOrderStateComplaining);


        tabAdapter = new FragmentTabAdapter(getSupportFragmentManager(), fragmentList);


    }

    @Override
    public void initView() {
        tvTitle.setText("交易订单");
        viewPager.setAdapter(tabAdapter);
        viewPager.setOffscreenPageLimit(5);//设置缓冲页面为4个,避免频繁销毁界面
        tabLayout.setupWithViewPager(viewPager);
        for (int i = 0; i < fragmentList.size(); i++) {
            TabLayout.Tab tab = tabLayout.getTabAt(i);
            tab.setCustomView(R.layout.content_order_tab);

            TextView tvName = (TextView) tab.getCustomView().findViewById(R.id.tv_name);
            tvName.setText(titleList.get(i));
            View vTap = (View) tab.getCustomView().findViewById(R.id.v_tap_name);
            if (i == 0) {
                vTap.setVisibility(View.VISIBLE);
            } else {
                vTap.setVisibility(View.INVISIBLE);
            }

        }
        tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                tab.getCustomView().findViewById(R.id.v_tap_name).setVisibility(View.VISIBLE);
                selectedIndex = tab.getPosition();
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {
                tab.getCustomView().findViewById(R.id.v_tap_name).setVisibility(View.INVISIBLE);

            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });

        tabLayout.getTabAt(0).select();
        viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                selectedIndex = position;
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }

    @Override
    public void initOperator() {

    }

    @Override
    protected void onResume() {
        super.onResume();
        Fragment fragment = fragmentList.get(selectedIndex);
        if(fragment != null){
             if(fragment instanceof FragmentOrderStateAskingBuy){
                 ((FragmentOrderStateAskingBuy)fragment).refresh();
             }else
             if(fragment instanceof FragmentOrderStateSelling){
                 ((FragmentOrderStateSelling)fragment).refresh();
             }else
             if(fragment instanceof FragmentOrderStateTrading){
                 ((FragmentOrderStateTrading)fragment).refresh();
             }else
             if(fragment instanceof FragmentOrderStateFinished){
                 ((FragmentOrderStateFinished)fragment).refresh();
             }else
             if(fragment instanceof FragmentOrderStateComplaining){
                 ((FragmentOrderStateComplaining)fragment).refresh();
             }

        }
    }

    @Override
    public void onBackPressed() {
        setResult(RESULT_OK);
        finish();
        super.onBackPressed();
    }

    @OnClick({R.id.iv_back})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.iv_back:
                onBackPressed();
                break;
        }
    }
}
